package com.example.dell.newstart;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.dell.newstart.base.BaseActivity;
import com.example.dell.newstart.cart.CartFragment;
import com.example.dell.newstart.home.HomeFragment;
import com.example.dell.newstart.my.MyFragment;
import com.example.dell.newstart.type.TypeFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity implements View.OnClickListener {
    private ViewPager viewPager;//用来左右滑动的控件
    private ImageView homeImageView, typeImageView, cartImageView, myImageView;
    private LinearLayout llHome, llType, llCart, llMy;
   private HomeFragment homeFragment = new HomeFragment();
    //TypeFragment typeFragment = new TypeFragment();
    //ypeFragment typeFragment = new TypeFragment();
    private TypeFragment typeFragment = new TypeFragment();
    private CartFragment cartFragment = new CartFragment();
    //将对象装到集合中
    private MyFragment myFragment = new MyFragment();

    private List<Fragment> fragmentList;//用来存放添加到ViewPage中的Fragment的集合


    public int initLayout() {
        //EnvUtils.setEnv(EnvUtils.EnvEnum.SANDBOX);
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        viewPager = findViewById(R.id.vp_my);//找到布局中对应的ViewPage
        homeImageView = findViewById(R.id.iv_home);//找到布局中对应的ImageView
        typeImageView = findViewById(R.id.iv_type);//找到布局中对应的ImageView
        cartImageView = findViewById(R.id.iv_cart);//找到布局中对应的ImageView
        myImageView = findViewById(R.id.iv_my);//找到布局中对应的ImageView

        llHome = findViewById(R.id.ll_home);
        llType = findViewById(R.id.ll_type);
        llCart = findViewById(R.id.ll_cart);
        llMy = findViewById(R.id.ll_my);
        //设置点击事件监听
        llHome.setOnClickListener(this);
        llType.setOnClickListener(this);
        llCart.setOnClickListener(this);
        llMy.setOnClickListener(this);


        fragmentList = new ArrayList<>();//初始化集合
        //获得对应的Fragment对象

        fragmentList.add(homeFragment);
        fragmentList.add(typeFragment);
        fragmentList.add(cartFragment);
        fragmentList.add(myFragment);
        //给ViewPage设置Fragment，（让能滑动切换不同的Fragment）
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return fragmentList.get(i);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }
        });
        //viewpage滑动监听（能让我们知道滑动到了哪一项）
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                setImageView(i);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    /**
     * 设置当某一项被选中时的逻辑
     *
     * @param i 选中的那一项的下标
     */
    private void setImageView(int i) {


        resetAllImageView();
        switch (i) {
            case 0:
                homeImageView.setImageResource(R.mipmap.homepage_fill);
                break;
            case 1:
                typeImageView.setImageResource(R.mipmap.label_fill);
                break;
            case 2:
                cartImageView.setImageResource(R.mipmap.like_fill);
                break;
            case 3:
                myImageView.setImageResource(R.mipmap.people_fill);
                break;
        }
    }

    //将所有ImageView的图片设为灰色
    private void resetAllImageView() {
        homeImageView.setImageResource(R.mipmap.homepage);
        typeImageView.setImageResource(R.mipmap.label);
        cartImageView.setImageResource(R.mipmap.like);
        myImageView.setImageResource(R.mipmap.people);
    }

    @Override
    public void initData() {

    }

    //点击事件发生之后，对于设置了监听的控件，执行该方法
    @Override
    public void onClick(View v) {


        switch (v.getId()) {
            case R.id.ll_home:
                selectedItem(0);
                break;
            case R.id.ll_type:
                selectedItem(1);
                break;
            case R.id.ll_cart:
                selectedItem(2);
                break;
            case R.id.ll_my:
                selectedItem(3);
                break;
        }
    }

    public ViewPager getViewPager() {
        return viewPager;
    }
    //供外部指定跳转到某个“页卡”，如从“购物车”跳转到“分类”的实现
    public void selectPage(int i) {
        viewPager.setCurrentItem(i);
    }

    private void selectedItem(int i) {

        setImageView(i);//处理图标的变化
        viewPager.setCurrentItem(i);//处理页面的变化
    }

    /*public void goLoginActivity(View view) {
        startActivity(new Intent(this, LoginActivity.class));
    }*/
}
